//
//  HWIntegralMallshopViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/10/22.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWIntegralMallshopViewController.h"
#import "HWShopMallModel.h"
#import "HWMallcheCollectionViewCell.h"
#import "HWShangpingxiangqingViewController.h"
#import "HWMyChangeduihuaViewController.h" // 我的兑换接口
#import "HWshimingrenzhengTakuanView.h"  //

#import "HWweixingtishiView.h"  // 温馨提示

#import "HWxiaohaojifenView.h"





@interface HWIntegralMallshopViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(strong,nonatomic)UIImageView *shangbanImageView;
@property(strong,nonatomic)UIImageView *xiabanbufenImageView;
@property(strong,nonatomic)UIView *integralbeijingView;
@property(strong,nonatomic) UIImageView  *beijingicoIamge;
@property(strong,nonatomic)UIButton *jifenBut;
@property(strong,nonatomic)UIButton *fengqindui;
@property(strong,nonatomic)UIView *shopbaisebeijingView;

@property(strong,nonatomic) UICollectionView *maincollectionView;
@property(strong,nonatomic) NSString *p; //分页
@property(strong,nonatomic) NSString *max_p; // 最大分页
@property(strong,nonatomic) NSMutableArray *arrayDS;
@property(strong,nonatomic) NSString *cunchuStr;
 




@end

@implementation HWIntegralMallshopViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
     NSLog(@"333333333");
  //   [self chaxunUserCenter];
    //最新接口
 //    [self zuixingqingqiushuju];
    [self.navigationController setNavigationBarHidden:YES];
    
    [self navigaView];
    
    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
     NSLog(@"444444444"); 
    [self.navigationController setNavigationBarHidden:NO];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
   //  [self addbaisekongjianView];
    [self addrefreshControl];
    

    
}



- (void)navigaView  {
   
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10,StatusBarHeight+17, 40, 17);
   // button.backgroundColor = [UIColor redColor];
    [button setImage:[UIImage imageNamed:@"details_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

//    UILabel *textlabel = [[UILabel alloc]initWithFrame:CGRectMake((kScreenW-70)/2, StatusBarHeight+17, 70, 17)];
//    textlabel.text = @"任务中心";
//    textlabel.textAlignment =  NSTextAlignmentCenter;
//    textlabel.textColor = [UIColor whiteColor];
//    textlabel.font = [UIFont systemFontOfSize:16];
//    [self.view addSubview:textlabel];
    
    
    
    
    
  //  jiludushao
    
//       UIButton *lishijilubutton = [UIButton buttonWithType:UIButtonTypeCustom];
//       lishijilubutton.frame = CGRectMake(kScreenW-35,StatusBarHeight+17, 17, 17);
//      // button.backgroundColor = [UIColor redColor];
//       [lishijilubutton setImage:[UIImage imageNamed:@"jiludushao"] forState:UIControlStateNormal];
//       [lishijilubutton addTarget:self action:@selector(jiludushaoTap) forControlEvents:UIControlEventTouchUpInside];
//       [self.view addSubview:lishijilubutton];
//
    
    
}


- (void)buttonTap {
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}



- (void)addrefreshControl {
    
    self.maincollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    [self.maincollectionView.mj_header beginRefreshing];
    self.maincollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
    
    
      
    
    
}





- (void)underRefresh {
    DLog(@"精选");
    
 
       self.p  = @"1";
 
       self.arrayDS = [[NSMutableArray alloc]init];
        NSString *uid = [HWLocallyStoredData getLoginUserID];
        int t =   [WMUtil  getRandomNumber:101 to:998];
        NSString *tString = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
    
      //  NSString *gidStr = [NSString stringWithFormat:@"%@",self.gameId];
        NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
        NSString *signMD5 = [WMUtil md5HexDigest:signStr];

    //     NSString *type = @"1";

        NSDictionary *parametes = @{
                       
                          @"uid":uid,
                          @"t":tString,
                          @"sign":signMD5,
                          @"member_id":[HWLocallyStoredData getCode],
                          @"language":[HWjianzhiduiObject returnLanguageAcquisition],
                          @"p" :self.p,
                          
        };


        [HWWebUtils do4GetWithParameters:parametes  hiddenparameterStr:HW_mall progress:nil success:^(id response) {
            
           
            
            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
            
            
             [self.maincollectionView.mj_header endRefreshing];
             [self.maincollectionView.mj_footer endRefreshing];
            
            if ([codeStr intValue] == 1) {
               // [MBProgressHUD showMessage:@"收藏成功" toView:self.view];

                self.arrayDS  = [HWShopMallModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
                [self.maincollectionView reloadData];
                
                  NSString *str = [NSString stringWithFormat:@"%@积分",response[@"message"]];
                
                self.cunchuStr = [NSString stringWithFormat:@"%@",response[@"message"]];
                
                  [self.jifenBut setAttributedTitle:[WMUtil  attributedStringWithText:str withRangText:@"积分" withColor:RGB(255, 255, 255) withFont:[UIFont systemFontOfSize:13]] forState:UIControlStateNormal];
                 [self.jifenBut layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:10];

                 self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                 self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                
             
              
            } else {
                
                
               // [MBProgressHUD showError:@"收藏失败"];
               [MBProgressHUD showError:response[@"message"]];
                
            }
         
            
            
        } failure:^(NSString *error) {
           
            
              [self.maincollectionView.mj_header endRefreshing];
              [self.maincollectionView.mj_footer endRefreshing];
            
            
            
        }];
    
    
    
    
    
    
    
}



- (void)onRefresh  {
    
    
    if ([self.p intValue] > [self.max_p intValue] ) {
         
        [MBProgressHUD showError:@"请稍后在加载数据"];
        
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        
        return ;
    }
  // self.arrayDS = [[NSMutableArray alloc]init];
         NSString *uid = [HWLocallyStoredData getLoginUserID];
           int t =   [WMUtil  getRandomNumber:101 to:998];
           NSString *tString = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
       
         //  NSString *gidStr = [NSString stringWithFormat:@"%@",self.gameId];
           NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
           NSString *signMD5 = [WMUtil md5HexDigest:signStr];
       
         
       //     NSString *type = @"1";

           NSDictionary *parametes = @{
                             
               
                             @"uid":uid,
                             @"t":tString,
                             @"sign":signMD5,
                             @"member_id":[HWLocallyStoredData getCode],
                             @"language":[HWjianzhiduiObject returnLanguageAcquisition],
                             @"p" :self.p,
                              
           };


       [HWWebUtils do4GetWithParameters:parametes  hiddenparameterStr:HW_mall progress:nil success:^(id response) {
           
          
           
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           

         //  [self.tableView.mj_header endRefreshing];
            [self.maincollectionView.mj_footer endRefreshing];
           
           if ([codeStr intValue] == 1) {
              // [MBProgressHUD showMessage:@"收藏成功" toView:self.view];
               
              NSMutableArray  *muArr   = [HWShopMallModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
               
              //  self.tableView.contentOffset = CGPointMake(0, self.arrayDS.count*self.view.frame.size.height);
            
               for (int i =0 ; i < muArr.count; i++) {
                    [self.arrayDS addObject:muArr[i]];

               }
               [self.maincollectionView reloadData];
               self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
               self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
               

             
           } else {
               
               
                // [MBProgressHUD showError:@"收藏失败"];
                 [MBProgressHUD showError:response[@"message"]];
               
           }
        
           
           
       } failure:^(NSString *error) {
          
           
           

              [self.maincollectionView.mj_header endRefreshing];
              [self.maincollectionView.mj_footer endRefreshing];
              [MBProgressHUD showError:@"收藏失败"];
           
       }];
   
    
    
    
  
    
    
    
    
    
}
















// 设置UI
- (void)setupUI {
    
    self.view.backgroundColor = [UIColor redColor];
    [self addShangbanImageView];
    [self addIntegralbeijingView];
    // 添加控件Uiview
    [self addbaisekongjianView];
    
    
}


- (void)addShangbanImageView  {
    
    
    self.shangbanImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 512*CFLScaleValue)];
    self.shangbanImageView.image = [UIImage imageNamed:@"jifenshangtupian"];
    self.shangbanImageView.userInteractionEnabled = YES;
  //   self.shangbanImageView.hidden = YES;
    [self.view addSubview:self.shangbanImageView];
    
    self.xiabanbufenImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 512*CFLScaleValue-1, kScreenW, kScreenH -400 )];
    self.xiabanbufenImageView.image = [UIImage imageNamed:@"xiabanfentupian"];
    self.xiabanbufenImageView.userInteractionEnabled = YES;
   //  self.xiabanbufenImageView.hidden = YES;
    [self.view  addSubview:self.xiabanbufenImageView];
    
    
}
// 添加积分UIview
- (void)addIntegralbeijingView  {
    
    
    
    self.integralbeijingView = [[UIView alloc]initWithFrame:CGRectMake(20, 210, kScreenW-40, 58)];
    self.integralbeijingView.layer.cornerRadius = 58/2;
    self.integralbeijingView.layer.masksToBounds = YES;
    self.integralbeijingView.backgroundColor = [UIColor whiteColor];
    self.integralbeijingView.userInteractionEnabled = YES;
    [self.view addSubview:self.integralbeijingView];
    
    self.beijingicoIamge = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 234, 58)];
    self.beijingicoIamge.image = [UIImage imageNamed:@"jifenbeijing"];
   //  self.beijingicoIamge.backgroundColor = RGB(247, 107, 9);
    self.beijingicoIamge.userInteractionEnabled = YES;
    [self.integralbeijingView addSubview:self.beijingicoIamge];
    
    UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake( 20, 0, 234-40, 58) Title:@"1000积分" Image:@"jifenqianshu" Font:30 Color:RGB(255, 255, 255) Target:self Selector:nil];
          
   // butt.titleLabel.attributedText = [WMUtil  attributedStringWithText:@"1000积分" withRangText:@"积分" withColor:RGB(255, 255, 255) withFont:[UIFont systemFontOfSize:13]];
    
   //  [butt setAttributedTitle:[WMUtil  attributedStringWithText:@"1000积分" withRangText:@"积分" withColor:RGB(255, 255, 255) withFont:[UIFont systemFontOfSize:13]] forState:UIControlStateNormal];
    [butt layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:10];
             // butt.backgroundColor = [UIColor redColor];
    self.jifenBut =butt;
  //   butt.hidden = YES;
    [self.beijingicoIamge addSubview:butt];
    
    
    NSString  *duihuan =     [HWjianzhiduiObject keylocalization:@"我的兑换"];
    
    
    
    UIButton *butt1  = [UIButton setButtonWithFrame:CGRectMake( 234, 0, self.integralbeijingView.width - 234, 58) Title:duihuan Image:@"kechakan" Font:16 Color:RGB(136, 136, 136) Target:self Selector:@selector(duihuanrrrr)];
     [butt1 layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:10];
    
    butt1.userInteractionEnabled = YES;
  //   butt1.hidden = YES;
    butt1.backgroundColor = [UIColor clearColor];
    self.fengqindui = butt1;
    
     [self.integralbeijingView addSubview:butt1];
    
 //    [butt1 addTarget:self action:@selector(fasong) forControlEvents:UIControlEventTouchUpInside];
    
    
     
 
    
    
    
}


- (void)duihuanrrrr {
    
    
    HWMyChangeduihuaViewController *mychang = [[HWMyChangeduihuaViewController alloc]init];
    [self.navigationController pushViewController:mychang animated:YES];


    
}

 


//添加账号
- (void)addbaisekongjianView {
    
    self.shopbaisebeijingView = [[UIView alloc]initWithFrame:CGRectMake(20, 210+58+10, kScreenW-40, kScreenH - 210-58-10-CF_TabbarSafeBottomMargin-20)];
    self.shopbaisebeijingView.backgroundColor = [UIColor whiteColor];
    self.shopbaisebeijingView.layer.cornerRadius = 10;
    self.shopbaisebeijingView.layer.masksToBounds = YES;
    [self.view addSubview:self.shopbaisebeijingView];
    
    [self addControlsview];
    
    
}

#pragma  mark -----  添加控件
- (void)addControlsview {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 10;
    
 //   CGFloat juanju  = (kScreenW - 24*CFLScaleValue*2-62*CFLScaleValue*3-78*CFLScaleValue)/2;
    layout.minimumInteritemSpacing = 10;
  //  layout.headerReferenceSize = CGSizeMake(kScreenW, 142);  //设置headerView大小
    self.maincollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 10, kScreenW-40, kScreenH - 210-58-10-CF_TabbarSafeBottomMargin-20) collectionViewLayout:layout];
    // self.maincollectionView.showsHorizontalScrollIndicator = NO;
    self.maincollectionView.delegate = self;
    self.maincollectionView.dataSource = self;
    self.maincollectionView.alwaysBounceVertical = YES;
   self.maincollectionView.backgroundColor = [UIColor whiteColor];
    
   // self.maincollectionView.backgroundColor =RGB(242, 242, 242);
    self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 10, 10, 10);
  //  self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 24*CFLScaleValue, 0, 24*CFLScaleValue);
 //  [self.maincollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
 //    [self.maincollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    [self.maincollectionView  registerNib:[UINib nibWithNibName:@"HWMallcheCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
    self.maincollectionView.userInteractionEnabled = YES;
    [self.shopbaisebeijingView  addSubview:self.maincollectionView];
    

    
}



#pragma mark --------- UICollectionViewDelegate


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
 
   //  return self.dataMuArr.count;
    
    return self.arrayDS.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HWMallcheCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
       HWShopMallModel *model = self.arrayDS[indexPath.row];
    cell.dataModel = self.arrayDS[indexPath.row];
    
    [cell.jifenbuzuBut setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
    
    
    
    if ([model.market_price floatValue] < [self.cunchuStr intValue] ) {
    
         cell.jifenbuzuBut.backgroundColor = RGB(248, 119, 6);
      //   cell.jifenbuzuBut.backgroundColor = []
        
          NSString *lijidui =   [HWjianzhiduiObject keylocalization:@"立即兑换"];
        
         [cell.jifenbuzuBut setTitle:lijidui forState:UIControlStateNormal];
           
        
         
    } else {
        
         cell.jifenbuzuBut.backgroundColor = RGB(204, 204, 204);
        
        NSString *jifenbuzudui =   [HWjianzhiduiObject keylocalization:@"积分不足"];
        
         [cell.jifenbuzuBut setTitle:jifenbuzudui forState:UIControlStateNormal];
        
    }
    //  cell.jifenbuzuBut
    
    
    
    [cell.jifenbuzuBut  addTarget:self action:@selector(duihuanBut:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    return cell;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((kScreenW-40-20-10)/2,152+40+20+4+22+15);
    
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"12334");
    
    HWShopMallModel *model = self.arrayDS[indexPath.row];
    

      HWShangpingxiangqingViewController *shangping = [[HWShangpingxiangqingViewController alloc]init];
      shangping.goodsID = model.goods_id;
      [self.navigationController pushViewController:shangping animated:YES];
    
    
    
    
}



- (void)duihuanBut:(UIButton *)sender {
    
    NSLog(@"%@",[[[sender superview] superview] superview]);
    
    
   
    
    
    
    HWMallcheCollectionViewCell *cell = (HWMallcheCollectionViewCell *)[[[sender superview] superview] superview];
    
    
    
     
    // 实名认证
//    HWshimingrenzhengTakuanView  *fenxiang = [[HWshimingrenzhengTakuanView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
//       //    fenxiang.urlStr = self.topModel.ios;
//          [kKeyWindow addSubview:fenxiang];
//
    
    
    
    // 积分不够
//    HWweixingtishiView *weixing = [[HWweixingtishiView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
//    [kKeyWindow addSubview:weixing];
    
    // 积分够
//        HWxiaohaojifenView *xiaohaoview = [[HWxiaohaojifenView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
//        [kKeyWindow addSubview:xiaohaoview];
//
//    return ;
    
        
//
    
    
 //    HWxiaohaojifenView
    
    
  
    
    NSIndexPath *indexPath = [self.maincollectionView indexPathForCell:cell];
    
    
    
      HWShopMallModel *model = self.arrayDS[indexPath.row];
    
    

    
          NSString *userID = [HWLocallyStoredData getLoginUserID];
             NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
             NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
             NSString *signMD5 = [WMUtil md5HexDigest:sign];
             NSDictionary *parametes = @{
                                        @"ct" :  @"app",
                                        @"ac" : @"mall_query",
                                        @"ios" : @"1",
                                        @"member_id":[HWLocallyStoredData getCode],
                                        @"uid":userID,
                                        @"id": model.goods_id,
                                        @"uid":[HWLocallyStoredData getLoginUserID],

                                        };
       
       
            
        //    [MBProgressHUD show:@"正在绑定邮箱"];
            
            [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
                
              
                NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
             //   [MBProgressHUD hideHUD];
                
                 // 1可以兑换  2.代表积分不足。3 代表未实名认证
                if ([codeStr intValue] == 1) {
                    // 1可以兑换
                    
                    
                //     HWxiaohaojifenView *xiaohaoview = [[HWxiaohaojifenView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
                 //      [kKeyWindow addSubview:xiaohaoview];
                    
                    
                    HWShangpingxiangqingViewController *shangping = [[HWShangpingxiangqingViewController alloc]init];
                       shangping.goodsID = model.goods_id;
                       [self.navigationController pushViewController:shangping animated:YES];
                     
              

                } else  if (([codeStr intValue] == 2)) {
                    
                     HWweixingtishiView *weixing = [[HWweixingtishiView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
                    [kKeyWindow addSubview:weixing];
                    
                    
                } else if (([codeStr intValue] == 3)) {
                    
                         HWshimingrenzhengTakuanView  *fenxiang = [[HWshimingrenzhengTakuanView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
                    //       //    fenxiang.urlStr = self.topModel.ios;
                               [kKeyWindow addSubview:fenxiang];
                    
                }
                    
                    
                    
              //   NSLog(@"%@",response);
                
                
            } failure:^(NSString *error) {

                [MBProgressHUD  showError:@"查询失败"];
                
               
            }];
    

    
 
    
    
}




 
 
@end
